import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import nprogress from 'nprogress' // @types/nprogress
import 'nprogress/nprogress.css'
import productRouter from './modules/product'
import Layout from '@/layout/index.vue'
import { store } from '@/store/index'

const routes: RouteRecordRaw = [
  {
    path: '/login',
    name: 'login',
    // permissions: ['admin', 'visitor', 'vip'],
    // icon: '',
    component: () => import('../views/login/Index.vue')
  },
  {
    path: '/demo',
    name: 'demo',
    component: () => import('../views/demo/Demo.vue'),
    meta: { title: 'demo' }
  },
  {
    path: '/',
    // name: 'Launch',
    component: Layout,
    redirect: '/',
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '/',
        name: 'home',
        component: () => import('../views/home/Index.vue'),
        meta: { title: '首页' }
      },
      {
        path: '/launch',
        name: 'launch',
        component: () => import('../views/launch/Index.vue'),
        meta: { title: 'Launch' }
      },
      {
        path: '/login',
        name: 'login',
        // permissions: ['admin', 'visitor', 'vip'],
        // icon: '',
        component: () => import('../views/login/Index.vue')
      },

      productRouter
    ]
  },
  {
    // 404跳转到首页(此配置必须处于路由最后一项)
    path: '/:catchAll(.*)',
    redirect: { name: 'home' }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from) => {
  nprogress.start() // 开始加载进度条
  // if (to.meta.requiresAuth && !store.state.user) {
  //   // 此路由需要授权，请检查是否已登录
  //   // 如果没有，则重定向到登录页面
  //   return {
  //     path: '/admin/login',
  //     // 保存我们所在的位置，以便以后再来
  //     query: { redirect: to.fullPath }
  //   }
  // }
})

router.afterEach(() => {
  nprogress.done() // 加载进度条
})

export default router
